<?php

namespace App\Console\Commands;

use App\Lib\faceTencent;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class TencentFaceRefreshToken extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */

    protected $signature = 'TencentFaceRefreshToken';

    protected $lockKey = 'TENCENT:FACE:REFRESH:CRONTAB';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新腾讯人脸token和ticket';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        set_time_limit(0);
    
        do {
            redis_lock($this->lockKey,60);
            // 刷新token和sign
            $this->refresh();
            redis_unlock($this->lockKey);
        } while (false);

    }
    
    // 更新token和ticket
    public function refresh(){
        $face = new faceTencent();
        $token = $face->refreshAccessToken();
        if ($token) {
            $ticket = $face->refreshSignTicket();
            if (!$ticket) {
                Redis::DEL($face->accessTokenKey);
            } else {
                echo "refresh success" . PHP_EOL;
            }
        }

    }


}
